package com.weitu.workflow.flowManagement.controller;

import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.user.entity.User;
import com.weitu.plugin.PageView;
import com.weitu.workflow.flowManagement.entity.WorkItemInfo;
import com.weitu.workflow.flowManagement.entity.WorkflowInstance;
import com.weitu.workflow.flowManagement.entity.WorkflowTaskInstance;
import com.weitu.workflow.flowManagement.service.FlowManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/workflow/flowManagement")
public class FlowManagementController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "workflow/flowManagement";
    @Autowired
    private FlowManagementService flowManagementService;//流程管理Service

    /***
     * 跳转到待办页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/todoList")
    public ModelAndView todoList(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/todoList");
        return modelAndView;
    }

    /***
     * 跳转到已办页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/haveDoneList")
    public ModelAndView haveDoneList(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/haveDoneList");
        return modelAndView;
    }

    /***
     * 查询工作项列表数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/findWorkItemList")
    public Object findFlowTodoList(HttpServletRequest request) {

        User currentUser = this.getSessionUser();//当前用户
        Map<String, Object> map = new HashMap<String, Object>();

        //获取前台参数
        this.putParametersIntoMap(map);
        map.put("userName", currentUser.getName());
        Integer totalNum = this.flowManagementService.queryWorkItemCount(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);
        List<WorkItemInfo> workItemList = this.flowManagementService.queryWorkItemList(map);

        return pagination.flushDataGridData(workItemList);
    }

    /***
     * 查询工作项列表数据数量
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/findWorkItemListCount")
    public Integer findWorkItemListCount(HttpServletRequest request) {

        User currentUser = this.getSessionUser();//当前用户
        Map<String, Object> map = new HashMap<String, Object>();
        //获取前台参数
        this.putParametersIntoMap(map);
        map.put("userName", currentUser.getName());
        return this.flowManagementService.queryWorkItemCount(map);//获取总数

    }

    /***
     * 查询业务表单地址
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryBusinessFormUrl")
    public Object queryBusinessFormUrl(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        //获取前台参数
        this.putParametersIntoMap(map);
        String workItemId = map.get("workItemId").toString();//工作项id
        int formType = map.get("formType") != null ? Integer.parseInt(map.get("formType").toString()) : 0;//表单类型
        String formUrl = this.flowManagementService.queryBusinessFormByWorkItemId(workItemId, formType);//业务表单地址
        if (formUrl != null) {
            formUrl = formUrl + (formUrl.indexOf("?") > 0 ? "&" : "?") + "workItemId=" + workItemId + "&formType=" + formType;
        }
        return formUrl;
    }

    /***
     * 跳转到业务页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/toBusinessPage")
    public ModelAndView toBusinessPage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();

        Map<String, Object> map = new HashMap<String, Object>();
        //获取前台参数
        this.putParametersIntoMap(map);
        String workItemId = map.get("workItemId").toString();//工作项id
        WorkItemInfo workItemInfo = this.flowManagementService.queryWorkItemInfoByWorkItemId(workItemId);//查询工作项信息
        String businessId = workItemInfo.getBusinessId();//工作流关联业务id

        int formType = map.get("formType") != null ? Integer.parseInt(map.get("formType").toString()) : 0;//表单类型
        String formUrl = this.flowManagementService.queryBusinessFormByWorkItemId(workItemId, formType);//业务表单地址
        formUrl = formUrl + (formUrl.indexOf("?") > 0 ? "&" : "?") + "workItemId=" + workItemId + "&formType=" + formType + "&businessId=" + businessId;
        modelAndView.addObject("formUrl", formUrl);

        modelAndView.setViewName(MODULE_BATH_PATH + "/businessPage");
        return modelAndView;
    }

    /***
     * 跳转到流程监控页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/flowMonitorByBusinessId")
    public ModelAndView flowMonitorByBusinessId(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String businessId = request.getParameter("businessId");
        Map<String, Object> workflowMonitorInfo = new HashMap<String, Object>();
        //查询流程实例
        WorkflowInstance workflowInstance = this.flowManagementService.queryWorkflowInstanceByBusinessId(businessId);
        workflowMonitorInfo.put("workflowInstance", workflowInstance);
        //查询任务实例
        List<WorkflowTaskInstance> workflowTaskInstanceList = this.flowManagementService.queryTaskInstanceListByWorkflowId(workflowInstance.getId());
        List<Map<String, Object>> taskList = new ArrayList<Map<String, Object>>();
        for (WorkflowTaskInstance workflowTaskInstance : workflowTaskInstanceList) {
            Map<String, Object> taskInstanceInfo = new HashMap<String, Object>();
            taskInstanceInfo.put("workflowTaskInstance", workflowTaskInstance);
            //查询工作项
            List<WorkItemInfo> workItemInfoList = this.flowManagementService.queryWorkItemInfoListByTaskInstanceId(workflowTaskInstance.getId());
            taskInstanceInfo.put("workflowItemInfoList", workItemInfoList);
            taskList.add(taskInstanceInfo);
        }
        workflowMonitorInfo.put("workflowTaskInstanceList", taskList);
        modelAndView.addObject("workflowInstance", workflowMonitorInfo);
        modelAndView.setViewName(MODULE_BATH_PATH + "/flowMonitor");
        return modelAndView;
    }

    /***
     * 跳转到流程监控页面,根据工作项id
     * @param request
     * @return
     */
    @RequestMapping(value = "/flowMonitorByWorkItemId")
    public ModelAndView flowMonitorByWorkItemId(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String workItemId = request.getParameter("workItemId");
        Map<String, Object> workflowMonitorInfo = new HashMap<String, Object>();
        //查询流程实例
        WorkflowInstance workflowInstance = this.flowManagementService.queryWorkflowInstanceByWorkItemId(workItemId);
        workflowMonitorInfo.put("workflowInstance", workflowInstance);
        //查询任务实例
        List<WorkflowTaskInstance> workflowTaskInstanceList = this.flowManagementService.queryTaskInstanceListByWorkflowId(workflowInstance.getId());
        List<Map<String, Object>> taskList = new ArrayList<Map<String, Object>>();
        for (WorkflowTaskInstance workflowTaskInstance : workflowTaskInstanceList) {
            Map<String, Object> taskInstanceInfo = new HashMap<String, Object>();
            taskInstanceInfo.put("workflowTaskInstance", workflowTaskInstance);
            //查询工作项
            List<WorkItemInfo> workItemInfoList = this.flowManagementService.queryWorkItemInfoListByTaskInstanceId(workflowTaskInstance.getId());
            taskInstanceInfo.put("workflowItemInfoList", workItemInfoList);
            taskList.add(taskInstanceInfo);
        }
        workflowMonitorInfo.put("workflowTaskInstanceList", taskList);
        modelAndView.addObject("workflowInstance", workflowMonitorInfo);
        modelAndView.setViewName(MODULE_BATH_PATH + "/flowMonitor");
        return modelAndView;
    }

    /***
     * 跳转到审批人员选择页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/toFlowNextUserSelectPage")
    public ModelAndView toFlowNextUserSelectPage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/selectUser");
        return modelAndView;
    }

}
